package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.DestinationProQuery;
import cn.wolfcode.luowowo.common.query.DestinationQuery;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 主页控制
 */
@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private ITravelService travelService;

    //@RequiredLogin
    @RequestMapping("")
    //访问目的地主页,查询目的地,跳转
    public String index(Model model) {
        //获取所有区域
        List<Region> regions = regionService.listAll();
        model.addAttribute("hotRegions", regions);
        return "/destination/index";
    }

    @RequestMapping("/listHotDestByRegionId")
    //查询热门目的地
    public String listHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model) {
        //获取热门目的地
        List<Destination> leftDests = null;
        List<Destination> rightDests = null;
        if (regionId == -1) {
            List<Destination> destinations = destinationService.listHotDestByParentId(1L);
            leftDests = destinations.subList(0, destinations.size() / 2);
            rightDests = destinations.subList(destinations.size() / 2, destinations.size());
        } else {
            List<Destination> destinations = destinationService.listHotDestByRegionId(regionId);
            leftDests = destinations.subList(0, destinations.size() / 2);
            rightDests = destinations.subList(destinations.size() / 2, destinations.size());
        }
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        return "/destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Long id, Model model) {
        //吐司
        List<Destination> toasts = destinationService.toastsList(id);
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);

        //查询所有攻略分类
        List<StrategyCatalog> catalogs = strategyCatalogService.selectByDestId(id);
        model.addAttribute("catalogs", catalogs);
        //查询点击量前三的文章
        List<StrategyDetail> details = strategyDetailService.selectViewNumTop3(id);
        model.addAttribute("strategyDetails", details);
        return "/destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(@ModelAttribute("qo") DestinationQuery qo, Model model) {
        //吐司
        List<Destination> toasts = destinationService.toastsList(qo.getDestId());
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);
        return "/destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(@ModelAttribute("qo") DestinationQuery qo, Model model) {
        //当前目的地下所有攻略分类(catalogs)
        List<StrategyCatalog> catalogs = strategyCatalogService.selectByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        //当前攻略分类

        for (StrategyCatalog catalog : catalogs) {
            if (catalog.getId() == qo.getCatalogId()) {
                model.addAttribute("catalog", catalog);
                //当前攻略分类下的第一篇文章
                List<StrategyDetail> details = strategyDetailService.selectByCatalogId(catalog.getId());
                if (details != null) {
                    StrategyDetail detail = details.get(0);
                    //设置内容
                    StrategyContent content = strategyContentService.get(detail.getId());
                    detail.setStrategyContent(content);
                    model.addAttribute("detail", detail);
                }
                break;
            }
        }
        return "/destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(@ModelAttribute("qo") TravelQuery qo, Model model) {
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "destination/travelTpl";
    }

    @RequestMapping("/getList")
    public String getList1(@ModelAttribute("qo") DestinationProQuery qo, Model model) {
        List<Destination> destinations = destinationService.queryDestination(qo);
        //   System.out.println(destinations);
        model.addAttribute("destinations", destinations);
        return "destination/destFilter";
    }

    @RequestMapping("/getList1")
    public String getList(@ModelAttribute("qo") DestinationProQuery qo, Model model) {

        List<Destination> destinations = destinationService.queryDestination(qo);
        //   System.out.println(destinations);
        model.addAttribute("destinations", destinations);
        return "destination/destFilterTpl";
    }

}
